﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebLayer;
using System.Net;
using HtmlAgilityPack;

namespace WebPageParser
{
    class ReservedTableEntry
    {
        public String Class = null;
        public String Date = null;
        public String Cancel = null;
    }

    class WaitListTableEntry
    {
        public String Class = null;
        public String Date = null;
    }

    

    class ReservationsWebPage: SiteWebPage
    {
        private ReservedTableEntry m_reservedTableHeader = null;
        public  ReservedTableEntry ReservedTableHeader
        {
            get
            {
                if (m_reservedTableHeader == null)
                {
                    Request();
                }
                return m_reservedTableHeader;
            }
        }

        private List<ReservedTableEntry> m_reservedTableContent = null;
        public  List<ReservedTableEntry> ReservedTableContent
        {
            get
            {
                if (m_reservedTableContent == null)
                {
                    Request();
                }
                return m_reservedTableContent;
            }
        }

        private WaitListTableEntry m_waitListTableHeader = null;
        public  WaitListTableEntry WaitListTableHeader
        {
            get
            {
                if (m_waitListTableHeader == null)
                {
                    Request();
                }
                return m_waitListTableHeader;
            }
        }

        private List<WaitListTableEntry> m_waitListTableContent = null;
        public  List<WaitListTableEntry> WaitListTableContent
        {
            get
            {
                if (m_waitListTableContent == null)
                {
                    Request();
                }
                return m_waitListTableContent;
            }
        }

        public ReservationsWebPage() :
            base("Reservations", GetActionPageUri("cancelations"))
        { }

        protected override void OnRequest(HttpWebRequest request)
        {
            m_reservedTableHeader = new ReservedTableEntry();
            m_reservedTableContent = new List<ReservedTableEntry>();
            m_waitListTableHeader = new WaitListTableEntry();
            m_waitListTableContent = new List<WaitListTableEntry>();

            base.OnRequest(request);
        }

        protected override void OnResponse(HttpWebRequest request, HttpWebResponse response)
        {
            base.OnResponse(request, response);

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(PageContent);

            HtmlNode body = doc.DocumentNode;            

            //Results html page has only one table. Select it;
            HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//table");

            if (nodes != null)
            {
                int reservedNodeIndex = 0;
                int waitListNodeIndex = 1;

                if (body.ChildNodes.Count >= 2 &&
                    body.ChildNodes[0].Name == "div" && body.ChildNodes[1].Name == "div")
                {
                    reservedNodeIndex = -1;
                    waitListNodeIndex = 0;
                }

                if (reservedNodeIndex >= 0 && nodes.Count > reservedNodeIndex)
                {
                    HtmlNode reservedNode = nodes[reservedNodeIndex];
                    /* 3 columns in a table */
                    HtmlTableParser tableParser = new HtmlTableParser(3, reservedNode);
                    tableParser.TransformTable<ReservedTableEntry>(m_reservedTableContent, TransformerToReserved);
                    m_reservedTableHeader = TransformerToReserved(tableParser.Header, tableParser.HtmlHeader);
                }

                if (waitListNodeIndex >= 0 && nodes.Count > waitListNodeIndex)
                {
                    HtmlNode waitListNode = nodes[waitListNodeIndex];
                    HtmlTableParser tableParser = new HtmlTableParser(2, waitListNode);
                    tableParser.TransformTable<WaitListTableEntry>(m_waitListTableContent, TransformerToWaitList);
                    m_waitListTableHeader = TransformerToWaitList(tableParser.Header, tableParser.HtmlHeader);
                }
            }
        }

        private ReservedTableEntry TransformerToReserved(HtmlTableRow textRow, HtmlTableRow htmlRow)
        {
            ReservedTableEntry entry = new ReservedTableEntry();
            entry.Class = textRow[0];
            entry.Date = textRow[1];
            entry.Cancel = "0";

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(htmlRow[2]);
            HtmlNode formNode = doc.DocumentNode.SelectSingleNode("//form");

            if (formNode != null)
            {
                HtmlNode inputNode = formNode.SelectSingleNode("//input");
                HtmlAttribute attribute = inputNode.Attributes["value"];
                if (attribute != null)
                {
                    entry.Cancel = attribute.Value;
                }
            }

            return entry;
        }

        private WaitListTableEntry TransformerToWaitList(HtmlTableRow textRow, HtmlTableRow htmlRow)
        {
            WaitListTableEntry entry = new WaitListTableEntry();
            entry.Class = textRow[0];
            entry.Date = textRow[1];        
            return entry;
        }
    }
}
